package com.xtj.workquene;


import com.rabbitmq.client.*;
import com.xtj.utils.RabbitMQUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Customer2 {
    //消费对象
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接对象
        Connection connection = RabbitMQUtils.getConnection();
        //获取连接中通道
       final  Channel channel = connection.createChannel();

       channel.basicQos(1);
        //通道绑定对应消息队列
        //参数1:队列名称，如果队列不存在，自动创建
        //参数2:b用于定义队列特性是否持久化,true持久化队列，false不持久化
        //参数3:b1是否独占队列，true开启独占队列，false关闭独占队列
        //参数4:b2是否在消费完成自动删除，true自动删除，false关闭自动删除
        //参数4:额外附加参数
        channel.queueDeclare("work",true,false,false,null);
        //消费消息
        channel.basicConsume("work",false,new DefaultConsumer(channel) {
        @Override
        public void handleDelivery (String consumerTag, Envelope envelope, AMQP.BasicProperties properties,byte[] body) throws IOException {
            System.out.println("消费者-2)=" + new String(body));
            channel.basicAck(envelope.getDeliveryTag(),false);
        }
    });
}
}
